var myopts = {
    width: 300,     // 信息窗口宽度
    height: 200,     // 信息窗口高度
//        title : "实时信息" , // 信息窗口标题
    panel: "panel",
    enableMessage: true,//设置允许信息窗发送短息
    searchTypes: [
        BMAPLIB_TAB_SEARCH,   //周边检索
        BMAPLIB_TAB_TO_HERE,  //到这里去
        BMAPLIB_TAB_FROM_HERE //从这里出发
    ]
};

var anchorX = 11;
var anchorY = 11;
var txtMenuItem = [
    {
        text: '放大',
        callback: function () {
            map.zoomIn()
        }
    },
    {
        text: '缩小',
        callback: function () {
            map.zoomOut()
        }
    },
    {
        text: '放置到最大级',
        callback: function () {
            map.setZoom(18)
        }
    },
    {
        text: '查看全国',
        callback: function () {
            map.setZoom(4)
        }
    },
    {
        text: '在此添加标注',
        callback: function (p) {
            var marker = new BMap.Marker(p), px = map.pointToPixel(p);
            map.addOverlay(marker);
        }
    }
];
var styleOptions = {
    strokeColor:"red",    //边线颜色。
    fillColor:"red",      //填充颜色。当参数为空时，圆形将没有填充效果。
    strokeWeight: 3,       //边线的宽度，以像素为单位。
    strokeOpacity: 0.8,	   //边线透明度，取值范围0 - 1。
    fillOpacity: 0.6,      //填充的透明度，取值范围0 - 1。
    strokeStyle: 'solid' //边线的样式，solid或dashed。
}
var drawingModeoverlays = [];
var carsModeoverlays = [];

function queryAngle(angles){

    var angle=parseInt(angles);
    if(angle==0){
        return "北"
    } else if(angle==90){
        return "东"
    } else if(angle==180){
        return "南"
    } else if(angle==270){
        return "西"
    }else if(angle>0 && angle<90){
        return "东北"
    }else if(angle>90 && angle<180){
        return "东南"
    }else if(angle>180 && angle<270){
        return "西南"
    }else if(angle>270){
        return "西北"
    }

}
//0绿色是跑的，1红色是异常的，2灰色是离线的,3蓝色是停止的，
function queryCarIcon(angles,flag){
    var angle=parseInt(angles);
    var icon_name="car_online0.fw";
    if(angle==0){
        if(flag==0){
            icon_name="car_online0.fw";
        }else if(flag==1){
            icon_name="car_online1_0.fw";
        }else if(flag==2){
            icon_name="car_online2_0.fw";
        }else if(flag==3){
            icon_name="car_online3_0.fw";
        }

    }else if(angle>0 && angle<45){
        if(flag==0){
            icon_name="car_online0_45.fw";
        }else if(flag==1){
            icon_name="car_online1_0_45.fw";
        }else if(flag==2){
            icon_name="car_online2_0_45.fw";
        }else if(flag==3){
            icon_name="car_online3_0_45.fw";
        }
    }else if(angle==45){
        if(flag==0){
            icon_name="car_online45.fw";
        }else if(flag==1){
            icon_name="car_online1_45.fw";
        }else if(flag==2){
            icon_name="car_online2_45.fw";
        }else if(flag==3){
            icon_name="car_online3_45.fw";
        }
    }else if(angle>45 && angle<90){
        if(flag==0){
            icon_name="car_online45_90.fw";
        }else if(flag==1){
            icon_name="car_online1_45_90.fw";
        }else if(flag==2){
            icon_name="car_online2_45_90.fw";
        }else if(flag==3){
            icon_name="car_online3_45_90.fw";
        }
    }else if(angle==90){
        if(flag==0){
            icon_name="car_online90.fw";
        }else if(flag==1){
            icon_name="car_online1_90.fw";
        }else if(flag==2){
            icon_name="car_online2_90.fw";
        }else if(flag==3){
            icon_name="car_online3_90.fw";
        }
    }else if(angle>90 && angle<135){
        if(flag==0){
            icon_name="car_online90_135.fw";
        }else if(flag==1){
            icon_name="car_online1_90_135.fw";
        }else if(flag==2){
            icon_name="car_online2_90_135.fw";
        }else if(flag==3){
            icon_name="car_online3_90_135.fw";
        }
    }else if(angle==135){
        if(flag==0){
            icon_name="car_online135.fw";
        }else if(flag==1){
            icon_name="car_online1_135.fw";
        }else if(flag==2){
            icon_name="car_online2_135.fw";
        }else if(flag==3){
            icon_name="car_online3_135.fw";
        }
    }else if(angle>135 && angle<180){
        if(flag==0){
            icon_name="car_online135_180.fw";
        }else if(flag==1){
            icon_name="car_online1_135_180.fw";
        }else if(flag==2){
            icon_name="car_online2_135_180.fw";
        }else if(flag==3){
            icon_name="car_online3_135_180.fw";
        }
    }else if(angle==180){
        if(flag==0){
            icon_name="car_online180.fw";
        }else if(flag==1){
            icon_name="car_online1_180.fw";
        }else if(flag==2){
            icon_name="car_online2_180.fw";
        }else if(flag==3){
            icon_name="car_online3_180.fw";
        }
    }else if(angle>180 && angle<225){
        if(flag==0){
            icon_name="car_online180_225.fw";
        }else if(flag==1){
            icon_name="car_online1_180_225.fw";
        }else if(flag==2){
            icon_name="car_online2_180_225.fw";
        }else if(flag==3){
            icon_name="car_online3_180_225.fw";
        }
    }else if(angle==225){
        if(flag==0){
            icon_name="car_online225.fw";
        }else if(flag==1){
            icon_name="car_online1_225.fw";
        }else if(flag==2){
            icon_name="car_online2_225.fw";
        }else if(flag==3){
            icon_name="car_online3_225.fw";
        }
    }else if(angle>225 && angle<270){
        if(flag==0){
            icon_name="car_online225_270.fw";
        }else if(flag==1){
            icon_name="car_online1_225_270.fw";
        }else if(flag==2){
            icon_name="car_online2_225_270.fw";
        }else if(flag==3){
            icon_name="car_online3_225_270.fw";
        }
    }else if(angle==270){
        if(flag==0){
            icon_name="car_online270.fw";
        }else if(flag==1){
            icon_name="car_online1_270.fw";
        }else if(flag==2){
            icon_name="car_online2_270.fw";
        }else if(flag==3){
            icon_name="car_online3_270";
        }
    }else if(angle>270 && angle<315){
        if(flag==0){
            icon_name="car_online270_315.fw";
        }else if(flag==1){
            icon_name="car_online1_270_315.fw";
        }else if(flag==2){
            icon_name="car_online2_270_315.fw";
        }else if(flag==3){
            icon_name="car_online3_270_315.fw";
        }
    }else if(angle==315){
        if(flag==0){
            icon_name="car_online315.fw";
        }else if(flag==1){
            icon_name="car_online1_315.fw";
        }else if(flag==2){
            icon_name="car_online2_315.fw";
        }else if(flag==3){
            icon_name="car_online3_315.fw";
        }
    }else if(angle>315 && angle<350){
        if(flag==0){
            icon_name="car_online315_350.fw";
        }else if(flag==1){
            icon_name="car_online1_315_350.fw";
        }else if(flag==2){
            icon_name="car_online2_315_350.fw";
        }else if(flag==3){
            icon_name="car_online3_315_350.fw";
        }
    }
    return icon_name+".png"
}
var levelmap=16;
var scrollFunc=function(e){
    e=e || window.event;
// var t1=document.getElementById("map");
// t1.value=map.getZoom();
// levelmap=map.getZoom();
}
/*注册事件*/
if(document.addEventListener){
    document.addEventListener('DOMMouseScroll',scrollFunc,false);
}//W3C
window.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome
var pinToPoint=null;
// 添加点：经纬度，车牌号，方向，状态（异常，在线，离线等弹窗的数据内容）lat, lng, carno, d, flag, content
function addMarker(lat, lng, carno, d, flag, content,panTo,p1) {
// this.cleanAll(map);
    var point = new BMap.Point(lng, lat);
    var o = this.makeMarker(point, carno, d, flag);
    map.addOverlay(o);
// alert(levelmap);
// map.centerAndZoom(point, 20);
    if(panTo){
// pinToPoint=point;
        map.centerAndZoom(point, levelmap);

        map.panTo(point);

    }

    addCarMarks(carno,o);
// var point = new BMap.Point(lng,lat);
// var infoWindow = new BMap.InfoWindow(content, opts);  // 创建信息窗口对象
// infoWindow.setHeight(600);
// map.openInfoWindow(infoWindow, point); //开启信息窗口
    this.addClickHandler(content, o);
// pintoMarker();
}
function pintoMarker() {
    if(pinToPoint){
        map.panTo(pinToPoint);
    }

}
//创建车辆图标
function makeMarker(point, carno, d, flag) {
    if (!d) {
        d = "0";
    }
    var o = new BMap.Marker(point);
    if (carno) {
        var label = new BMap.Label(carno, {offset: new BMap.Size(20, -20)});
        label.setStyle({
// height:"120px", //高度
// width:"125px", //宽
            color : "red",
            fontSize : "12px",
            height : "20px",
// width : "100px",
            lineHeight : "20px",
            fontFamily:"微软雅黑"
        });
// label.setStyle({
//     // color : "#fff",
//     // fontSize : "16px",
//     width:"200px",
//     backgroundColor :"1",
//     // border :"0",
//     // fontWeight :"bold"
//     color : "#cb4546",
//     fontSize : "16px",
//     backgroundColor :"0.8",
//     border :"1",
//     // fontWeight :"bold"
// });
        o.setLabel(label);
        var angleIcon = queryCarIcon(d, flag);
        var myIcon = new BMap.Icon(carIocnPath + angleIcon, new BMap.Size(44, 44),{
            anchor: new BMap.Size(anchorX,anchorY),
        });
        o.setIcon(myIcon);
    }
    return o;
}
//    提示窗口
function addClickHandler(content, marker,myopts) {
    marker.addEventListener("click", function (e) {
            if(content){
                openInfo(content, e, marker,myopts)
            }
        }
    );
}
//    信息窗口
function openInfo(content, e, marker,myopts) {
    var p = e.target;
    var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
    var infoWindow = new BMap.InfoWindow(content, opts);  // 创建信息窗口对象
    if(myopts){
        infoWindow = new BMap.InfoWindow(content, myopts);  // 创建信息窗口对象
    }
// infoWindow.setHeight(600);
    map.openInfoWindow(infoWindow, point); //开启信息窗口
// infoWindow.redraw();
}

/*
 * 初始化地图
 */
function initPoint(jw){
    var array=[];
    var point=null;
    var pointjw = jw;
    if(pointjw!=null&&pointjw!=""){
        array=pointjw.split(",");
        point = new BMap.Point(array[1], array[0]);
    }else{
        point = new BMap.Point(120.417544, 36.189805);
    }
    map.centerAndZoom(point,15);
//        var myCity = new BMap.LocalCity();
//        myCity.get(myCityFn);   //创建一个LocalCity对象myCity，然后调用其get()方法，就得到了用户IP对应的城市。该城市结果会以参数形式传递给回调函数

}
function myCityFn(result){
    var cityName = result.name;
    map.setCenter(cityName);   //关于setCenter()可参考API文档---”传送门“
}

//参数 type=0车辆
//no:车牌号
function findmark(type,no) {
    var mark=null;
    if(type==0){
        mark= lastCarMark[no]
    }
    return mark;
}

//删除全部marker
function deleteAllPoint(){

    if(lastMark!=null && lastMark.length>0){
        for(var i=0;i<lastMark.length ; i++){
            map.removeOverlay(lastMark[i]);
        }
    }
    cleanCarMarks();
}
// 添加点到数组
function addCarMarks(carno,marker) {
    lastCarMark[carno]=marker;
    lastMark.push(marker);
    carsModeoverlays.push(marker);

}
// 删除存储的点
function cleanCarMarks() {
    lastMark=[];
    lastCarMark={}
}
//清除地图所有数据
function cleanAll() {
    for(var i = 0; i < carsModeoverlays.length; i++){
        map.removeOverlay(carsModeoverlays[i]);
    }
    carsModeoverlays.length = 0
// map.clearOverlays();

}
function cleanAllHis() {
// debugger;
// for(var i = 0; i < carsModeoverlays.length; i++){
//     map.removeOverlay(carsModeoverlays[i]);
// }
// carsModeoverlays.length = 0
    map.clearOverlays();

}
//清除点
function cleanAllMarkers() {
    map.removeOverlay(mark);
}
function addHismapMark(jd,wd,content) {
    var point = new BMap.Point(jd, wd);
    var o = new BMap.Marker(point);
    if(lastHisMak){
        map.removeOverlay(lastHisMak);
    }
    map.addOverlay(o);
// alert(levelmap);
// map.centerAndZoom(point, levelmap);
    map.centerAndZoom(point, 16);
    map.panTo(point);
// this.addClickHandler(content, o);
    var infoWindow = new BMap.InfoWindow(content, opts);  // 创建信息窗口对象
    map.openInfoWindow(infoWindow, point); //开启信息窗口
    lastHisMak=o;

}
// 清除选中车辆
function delCarNo(carno) {
    var mark=findmark(0,carno);
    map.removeOverlay(mark);
}

// 动态设置icon
function setMarkIcon(no,type,marker,callbacktype) {
    var flag=true;
    var icon_name="";
    var icon_open='light-open';
    var icon_close='light-close';
    var icon_warning='light-warning';
// var myIcon = new BMap.Icon("/css/mainFrame/img/"+icon_name+".png", new BMap.Size(20,50));
    if(marker.getIcon()){
        var imageUrl=marker.getIcon().imageUrl;
        if(imageUrl){
            var img_icon_name=imageUrl.substring(imageUrl.lastIndexOf('/')+1)
            if(img_icon_name=="light-warning-02.png"|| img_icon_name=="light-warning-01.png"||img_icon_name=="terminal-warning-01.png"){
                flag=false;
            }
        }
    }
    if(flag){
        marker.setIcon(myIcon);
    }
}
//实例化鼠标绘制工具
function initDrawingManager() {

    var drawingManager = new BMapLib.DrawingManager(map, {
        isOpen: false, //是否开启绘制模式
        enableDrawingTool: true, //是否显示工具栏
        drawingToolOptions: {
            anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
            offset: new BMap.Size(5, 5), //偏离值
        },
        circleOptions: styleOptions, //圆的样式
        polylineOptions: styleOptions, //线的样式
        polygonOptions: styleOptions, //多边形的样式
        rectangleOptions: styleOptions //矩形的样式
    });
    drawingManager.addEventListener('overlaycomplete', overlaycomplete);
    return drawingManager;
}


//添加鼠标绘制工具监听事件，用于获取绘制结果
var overlaycomplete = function(e){

    var pstr="";
    var type="1";
    var points;
    var point_marker;
    var center;
    var radius;

////线
    if(e.drawingMode=="polyline"){
// 折线点数组
        points=e.overlay.getPath();
        $.each(points,function(key,value){
            pstr=pstr+value.lng+","+value.lat+";";
        });
        if(pstr){
            pstr=pstr.substring(0,pstr.length-1)
        }
        var centerPoint=e.overlay.getBounds().getCenter();
        center=centerPoint.lng+","+centerPoint.lat;
        type="4";
    }
// 圆形
    if(e.drawingMode=="circle"){
// 中心
        center=e.overlay.getCenter();
// 半径
        radius=e.overlay.getRadius();
        pstr=center.lng+","+center.lat+";"+radius;
        center=pstr;
        type="2";
    }
//正方形
    if(e.drawingMode=="rectangle"){
        points=e.overlay.getPath();
        $.each(points,function(key,value){
            pstr=pstr+value.lng+","+value.lat+";";
        });
        if(pstr){
            pstr=pstr.substring(0,pstr.length-1)
        }
        type="3";
        var centerPoint=e.overlay.getBounds().getCenter();
        center=centerPoint.lng+","+centerPoint.lat;
    }
//多边形
    if(e.drawingMode=="polygon"){
        points=e.overlay.getPath();
        $.each(points,function(key,value){
            pstr=pstr+value.lng+","+value.lat+";";
        });
        if(pstr){
            pstr=pstr.substring(0,pstr.length-1)
        }
        type="3";
        var centerPoint=e.overlay.getBounds().getCenter();
        center=centerPoint.lng+","+centerPoint.lat;
    }
    if(e.drawingMode=="marker"){
        point_marker=e.overlay.getPosition();
        pstr=point_marker.lng+","+point_marker.lat;
        type="1";
        center=pstr;
    }
    drawingModeoverlays.push(e.overlay);
// 1-兴趣点 2-区域圆形 3-区域多边形 4-路线折线
    addFence(pstr,type,center);
};

var openRectangle=function (e1,e2) {

// 打开窗口
}
//保存矩形
var saveRectangle=function(fn){
    var args=[];
    if(fn && args){
        args[0]=this.rectangle_t;
        args[1]=this.rectangle_b;
        fn(args);
    }
}
//保存点
var savePointMarker=function(fn){
    var args=[];
    if(fn && args && this.point_marker){
        args=this.point_marker;
        fn(args);
    }
}
function settxtMenuItem(contextMenu) {
    for (var i = 0; i < txtMenuItem.length; i++) {
        contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text, txtMenuItem[i].callback, 100));
        if (i == 1 || i == 3) {
            contextMenu.addSeparator();
        }
    }
    return contextMenu
}
function clearAlldrawingModes() {
    for(var i = 0; i < drawingModeoverlays.length; i++){
        map.removeOverlay(drawingModeoverlays[i]);
    }
    drawingModeoverlays.length = 0
}
function addDrawingMark(lng, lat) {
    var point = new BMap.Point(lng, lat);
    var marker = new BMap.Marker(point);
    map.addOverlay(marker);
    map.centerAndZoom(point, 16);
    map.panTo(point);
    drawingModeoverlays.push(marker);
}
function addDrawingCircle(lng, lat,radius) {

    var point = new BMap.Point(lng, lat);
    var circle = new BMap.Circle(point, radius, styleOptions);
// var marker = new BMap.Marker(point);
    map.addOverlay(circle);
    map.centerAndZoom(point, 16);
    map.panTo(point);
    drawingModeoverlays.push(circle);
}
function addDrawingPolygon(pointsArray,lng, lat) {
    var points=[];
    var point = new BMap.Point(lng, lat);
    $.each(pointsArray,function(key,value){
        var array=value.split(",")
        var tmp_point = new BMap.Point(array[0], array[1]);
        points.push(tmp_point);
    });
    var polygon = new BMap.Polygon(points,styleOptions)
// var marker = new BMap.Marker(point);
    map.addOverlay(polygon);
    map.centerAndZoom(point, 16);
    map.panTo(point);
    drawingModeoverlays.push(polygon);
}
function addDrawingPolyline(pointsArray,lng, lat) {
    var points=[];
    var point = new BMap.Point(lng, lat);
    $.each(pointsArray,function(key,value){
        if(value){
            var array=value.split(",");
            var tmp_point = new BMap.Point(array[0], array[1]);
            points.push(tmp_point);
        }

    });
    var polyline = new BMap.Polyline(points,styleOptions)
// var marker = new BMap.Marker(point);
    map.addOverlay(polyline);
    map.centerAndZoom(point, 16);
    map.panTo(point);
    drawingModeoverlays.push(polyline);
}


//历史轨迹
var points=[];
var times=[];
var locations=[];
var baseCarNos=[];
var angles=[];
var accFlags=[];
var oneHighs=[];
var gpsPowerStatuss=[];
var velocitys=[];
var car =null;
var overlays= [];
var carlabel;
var index=0;
var timer;
var carstatusArr = [];
var carAddresss = [];
function cleanHis() {
    points=[];
    times=[];
    locations=[];
    baseCarNos=[];
    angles=[];
    velocitys=[];
    accFlags=[];
    oneHighs=[];
    gpsPowerStatuss=[];
    car =null;
    carsModeoverlays = [];
    carstatusArr = [];
    carAddresss = [];
    overlays= [];
    index=0;
}
//纬度,精度,方向,时间,速度
function addHisData(lng, lat,angle, time, velocity,baseCarNo,accFlag,oneHigh,gpsPowerStatus,carstatus,address) {
    var point = new BMap.Point(lng, lat);
    points.push(point);
    angles.push(angle);
    baseCarNos.push(baseCarNo);

    times.push(time);
    accFlags.push(accFlag);
    oneHighs.push(oneHigh);
    gpsPowerStatuss.push(gpsPowerStatus);
    carstatusArr.push(carstatus);
    carAddresss.push(address);
    velocitys.push(velocity);
    var myGeo = new BMap.Geocoder();
    var addressDetail = "无详细地址信息!";
    myGeo.getLocation(new BMap.Point(lng, lat), function (result) {
        if (result) {
            addressDetail = result.address;
        }

        locations.push(addressDetail);
// label.setContent("车牌号: " + baseCarNos[index] + "<br>速度: " + velocitys[index] + "<br>时间: " + times[index]+ "<br>位置: "+ locations[index]);


    });

}

function drawpolygon(lowspeed,highspeed) {
// var label; //信息标签

    var centerPoint;
//var followChk, playBtn, pauseBtn, resetBtn; //几个控制按钮


    if (points.length == 0) {
        swal({
            title: "系统提示",
            text: "该时间范围内无车辆数据",
            type: "error"
        });
        return false;
    }
    var pstart = points[0];
    var pend = points[points.length - 1];
    var polyline;
    var polylines=[];
    if(lowspeed&&highspeed){
        var lspeed=parseInt(lowspeed);
        var hspeed=parseInt(highspeed);
        var temps=[];
        var temp;
        var tempsp;
        var strokeColor="#05b801";//正常
// var strokeColor="#d9534f";高速
// var strokeColor="#428bca";低速
//         $.each(points,function(key,value){
//
//         });

        for(var m=0;m<points.length;m++){
            var key = m;
            var value = points[m];
            var sp=velocitys[key];
            if(sp<lspeed) {
                strokeColor = "#0010ca"
            }else if(sp>hspeed){
                strokeColor = "#d90109"
            }else{
                strokeColor="#05b801"
            }
            temps.push(value);
            if(key>0){
                polyline = new BMap.Polyline(temps, {strokeColor:strokeColor, strokeWeight: 5, strokeOpacity: 0.8});   //创建折线
                polylines.push(polyline);
                map.addOverlay(polyline);
                temps=[];
                temps.push(value);
                carsModeoverlays.push(polyline);
            }
        }
        overlays.push(polylines);

    }else{
        var polyline = new BMap.Polyline(points, {strokeColor: "red", strokeWeight: 4, strokeOpacity: 0.5});   //创建折线
        map.addOverlay(polyline);
        overlays.push(polyline);
        carsModeoverlays.push(polyline);
    }

// carlabel = new BMap.Label("经度: " + points[0].lng + "<br>纬度: " + points[0].lat + "<br>速度: " + velocitys[0] + "<br>时间: " + times[0]+ "<br>位置: "+ locations[0], {offset: new BMap.Size(-50, -80)});
// carlabel = new BMap.Label("车牌号: " + baseCarNos[0] + "<br>速度: " + velocitys[0] + "<br>时间: " + times[0]+ "<br>位置: "+ locations[0], {offset: new BMap.Size(-50, -80)});
// carlabel.setStyle({
//     // color: "red",
//     // fontSize: "12px",
//     // height: "80px",
//     // width: "180px",
//     // lineHeight: "20px",
//     // fontFamily: "微软雅黑"
//     color : "#000",
//     fontSize : "16px",
//     backgroundColor :"0.05",
//     border :"0",
//     fontWeight :"bold"
// });
    var angleIcon = queryCarIcon(angles[0],0);

    var myIcon = new BMap.Icon(carIocnPath + angleIcon, new BMap.Size(44, 44),{
        anchor: new BMap.Size(anchorX,anchorY),
    });
    car = new BMap.Marker(pstart, {icon: myIcon});
    carsModeoverlays.push(car);
// car = new BMap.Marker(pstart);
// car.setLabel(carlabel);
    var gpsPowerStatus = gpsPowerStatuss[index];
    var acc = accFlags[index];
    var oneHigh = oneHighs[index];
    var str="";
    var carstatus = carstatusArr[index];
    if(gpsPowerStatus == 1){
        str="主电正常, "
    }else{
        str="主电异常, "
    }
    if (acc == 1) {
        str = str+"点火, "
    }else{
        str = str+"熄火, "
    }
    if(oneHigh){
        str=str+" 时长: "+oneHigh+"分钟"
    }else{
        str=str+" 时长: 0 分钟"
    }
    var opts1 = {
        width : 200,     // 信息窗口宽度
        height: 150,     // 信息窗口高度
        title : '<h5>接收时间：' + times[index] + '</h5>'  , // 信息窗口标题
        enableMessage:true,//设置允许信息窗发送短息
// message:"亲耐滴，晚上一起吃个饭吧？戳下面的链接看下地址喔~"
    };
    var _address = locations[index] ? locations[index] : (carAddresss[index] ? carAddresss[index] : "暂无位置信息");
    var content = '<div class="">' +
        // '<div class="">' +
        // '<h5>接收时间：' + times[index] + '</h5>' +
        // '</div>' +
        '<div class="carRealTimeInfo"> ' +
        //                            '<div class="ibox-content ">' +
        '车牌号：<span class="carBaseNo">' + baseCarNos[index]  + '</span><br/>' +
        '方向：<span class="carDirect">' + queryAngle(angles[index]) + '</span><br/>' +
        '速度：<span class="carSpeed">' + velocitys[index] + '</span><br/>' +
        '时间:<span class="carTime">' +  times[index] + '</span><br/>' +
        '状态:<span class="carStatus">' +  carstatus + '</span><br/>' +
        '位置:<span class="carAddress">' + _address + '</span><br/>' +
        '</div>';
    this.addClickHandler(content, car,opts1);

    map.addOverlay(car);
    map.centerAndZoom(pstart, 15);
    map.panTo(pstart);

}
function onPlay () {
    var angleIcon = null;
    var myIcon;
    var label = carlabel;
    //points.length == 0
    //$("#serchStr").val()==""
    if (points.length == 0) {
        // swal({
        //     title: "系统提示",
        //     text: "请选择车辆或录入手机号查询轨迹",
        //     type: "error"
        // });
        //TODO 停止定时器

        if(timer){
            window.clearTimeout(timer);
        }
        // $('body').stopTime ('A');
        // $("#playbtn").show();
        // $("#pausebtn").hide();
        // $("#stopbtn").hide();
        // task.destroy();
        return false;
    }
    var point = points[index];
    // if (index > 0 && point) {
    //     map.addOverlay(new BMap.Polyline([points[index - 1], point], {
    //         strokeColor: "red",
    //         strokeWeight: 1,
    //         strokeOpacity: 1
    //     }));
    // }
    if (!point) {
        if(timer){
            window.clearTimeout(timer);
        }
        // $('body').stopTime ('A');
        swal({
            title: "系统提示",
            text: "轨迹查询完毕",
            type: "info"
        });
        // $("#playbtn").show();
        // $("#pausebtn").hide();
        // $("#stopbtn").hide();
        $("#progressbar-box").hide();
        $(".goonplayop").hide();
        $(".noplayop").hide();
        $(".playop").show();
        onReset();
        // Ext.MessageBox.alert('系统提示', '轨迹查询完毕!');
        // task.destroy();
        return false;
    }
    // label.setContent("经度: " + point.lng + "<br>纬度: " + point.lat + "<br>速度: " + velocitys[index] + "<br>时间: " + times[index]+ "<br>位置: "+ locations[index]);
    // label.setContent("车牌号: " + baseCarNos[index] + "<br>速度: " + velocitys[index] + "<br>时间: " + times[index]+ "<br>位置: "+ locations[index]);
    if(points.length){
        $("#pointingNum").text(index);
        var _percentage = (index/(points.length-1))*100;
        $("#progressbar").setProgress(_percentage);

    }
    car.setPosition(point);
    var angleIcon = queryCarIcon(angles[index],0);
    var myIcon = new BMap.Icon(carIocnPath + angleIcon, new BMap.Size(44, 44),{
        anchor: new BMap.Size(anchorX,anchorY),
    });
    car.setIcon(myIcon);
    index++;
    //        if(followChk.checked) {
    map.panTo(point);

    var gpsPowerStatus = gpsPowerStatuss[index];
    var acc = accFlags[index];
    var oneHigh = oneHighs[index];
    var carstatus = carstatusArr[index];
    var str=""
    if(gpsPowerStatus == 1){
        str="主电正常, "
    }else{
        str="主电异常, "
    }
    if (acc == 1) {
        str = str+"点火, "
    }else{
        str = str+"熄火, "
    }
    if(oneHigh){
        str=str+" 时长: "+oneHigh+"分钟"
    }else{
        str=str+" 时长: 0 分钟"
    }
    var _address = locations[index] ? locations[index] : (carAddresss[index] ? carAddresss[index] : "暂无位置信息");
    var content = '<div class="ibox float-e-margins">' +
        '<div class="">' +
        '<h5>实时信息</h5>' +
        '</div>' +
        '<div class="ibox-content"> ' +
        //                            '<div class="ibox-content ">' +
        '车牌号：<span class="carBaseNo">' + baseCarNos[index]  + '</span><br/>' +
        '方向：<span class="carDirect">' + queryAngle(angles[index]) + '</span><br/>' +
        '速度：<span class="carSpeed">' + velocitys[index] + '</span><br/>' +
        '时间:<span class="carTime">' +  times[index] + '</span><br/>' +
        '状态:<span class="carStatus">' +  carstatus + '</span><br/>' +
        '位置:<span class="carAddress">' + _address + '</span><br/>' +
        '</div>';
    this.addClickHandler(content, car,myopts);
    if(index > 0 && point){
        // if(ssss){
        //     timer=window.setTimeout("onPlay("+ss+")", ssss);
        // }else{
        window.clearTimeout(timer);
        timer=window.setTimeout("onPlay()", ss);
        // }

    }
    // if(index==points.length-1){
    //     if(timer){
    //         window.clearTimeout(timer);
    //     }
    //     // $('body').stopTime ('A');
    //     $("#playbtn").show();
    //     $("#pausebtn").hide();
    //     $("#stopbtn").hide();
    //     // onReset();
    // }

    //        }
    //        if(index < points.length) {
    //            this.timer=runner.start({
    //                run: this.play,
    //                interval: 1000
    //            });
    //            //this.timer = window.setTimeout("play(" + index + ")", 200);
    //        }
}
function onClose () {

    // cleanAll();
    cleanAllHis();
    //停止定时器
    // if (task) {
    //     task.destroy();
    //
    // }
    index = 0;
}
function onReset () {

    var angleIcon = null;
    var myIcon;

    var label = carlabel;
    //points.length == 0
    //$("#serchStr").val()==""
    if (points.length == 0) {
        // swal({
        //     title: "系统提示",
        //     text: "请选择车辆或录入手机号查询轨迹",
        //     type: "error"
        // });
        if(timer){
            window.clearTimeout(timer);
        }
        //停止定时器
        // task.destroy();
        // window.clearTimeout(timer);
        return false;
    }

    //停止定时器
    // if (task) {
    //     task.destroy();
    //     // task=null;
    //     //window.clearTimeout(task);
    // }
    index = 0;
    ss=t_s;
    car.setPosition(points[0]);
    var angleIcon = queryCarIcon(angles[0],0);
    var myIcon = new BMap.Icon(carIocnPath + angleIcon, new BMap.Size(44, 44),{
        anchor: new BMap.Size(anchorX,anchorY),
    });
    car.setIcon(myIcon);

    // label.setContent("经度: " + points[0].lng + "<br>纬度: " + points[0].lat + "<br>速度: " + velocitys[0] + "<br>时间: " + times[0]+ "<br>位置: "+ locations[0]);
    // label.setContent("车牌号: " + baseCarNos[0] + "<br>速度: " + velocitys[0] + "<br>时间: " + times[0]+ "<br>位置: "+ locations[0]);
    //        label.setOffset(10, 10);
    // car.setLabel(label);
    map.panTo(this.car);
    var gpsPowerStatus = gpsPowerStatuss[index];
    var acc = accFlags[index];
    var oneHigh = oneHighs[index];
    var carstatus = carstatusArr[index];
    var str=""
    if(gpsPowerStatus == 1){
        str="主电正常, "
    }else{
        str="主电异常, "
    }
    if (acc == 1) {
        str = str+"点火, "
    }else{
        str = str+"熄火, "
    }
    if(oneHigh){
        str=str+" 时长: "+oneHigh+"分钟"
    }else{
        str=str+" 时长: 0 分钟"
    }
    var _address = locations[index] ? locations[index] : (carAddresss[index] ? carAddresss[index] : "暂无位置信息");
    var content = '<div class="ibox float-e-margins">' +
        '<div class="">' +
        '<h5>实时信息</h5>' +
        '</div>' +
        '<div class="ibox-content"> ' +
        '车牌号：' + baseCarNos[index]  + '<br/>' +
        '方向：' + queryAngle(angles[index]) + '<br/>' +
        '速度：' + velocitys[index] + '<br/>' +
        '时间:' +  times[index] + '<br/>' +
        '状态:' +  carstatus + '<br/>' +
        '位置:' + _address + '<br/>' +
        '</div>';
    this.addClickHandler(content, car,myopts);
    if(timer){
        window.clearTimeout(timer);
    }
    return false;
}
function onPause() {

    if (points.length == 0) {
        // swal({
        //     title: "系统提示",
        //     text: "请选择车辆或录入手机号查询轨迹",
        //     type: "error"
        // });
        if(timer){
            window.clearTimeout(timer);
        }

        // Ext.MessageBox.alert('错误', '请选择车辆或录入手机号查询轨迹!');
        // task.destroy();
        return false;
    }
    //停止定时器
    // if (task) {
    //     task.stop();
    //     //window.clearTimeout(task);
    // }
}

//坐标-地址
function jwToAddress(jd,wd){
    var myGeo = new BMap.Geocoder();
    // 根据坐标得到地址描述
    myGeo.getLocation(new BMap.Point(jd, wd), function(result){
        if (result){
            return result.address;
        }else{
            return "无详细地址信息!"
        }
    });
}
function ZoomControl(){
    // 默认停靠位置和偏移量
    this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
    this.defaultOffset = new BMap.Size(50, 50); // 距离左上角位置
}
ZoomControl.prototype = new BMap.Control();

// 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
// 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
ZoomControl.prototype.initialize = function(map) {
    // 创建一个DOM元素
    // var x='<div class="contact-box"> <a href="profile.html"> <div class="col-sm-4"> <div class="text-center"> <img alt="image" class="img-circle m-t-xs img-responsive" src="img/a2.jpg"> <div class="m-t-xs font-bold">CTO</div> </div> </div> <div class="col-sm-8"> <h3><strong>奔波儿灞</strong></h3> <p><i class="fa fa-map-marker"></i> 甘肃·兰州</p> <address> <strong>Baidu, Inc.</strong><br>E-mail:xxx@baidu.com<br>Weibo:<a href="">http://weibo.com/xxx</a><br> <abbr title="Phone">Tel:</abbr> (123) 456-7890 </address> </div> <div class="clearfix"></div> </a> </div>'
    var div = document.createElement("div");
    // 添加文字说明
    // div.appendChild(document.createTextNode("放大2级"));
    // 设置样式
    div.style.cursor = "pointer";
    div.style.border = "1px solid gray";
    div.style.backgroundColor = "red";
    // 绑定事件,点击一次放大两级
    div.onclick = function (e) {
        map.setZoom(map.getZoom() + 2); // 动作
    }
    // 添加DOM元素到地图中
    map.getContainer().appendChild(div);
    // 将DOM元素返回
    return div;
}